package com.bofa.dao;

import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.bofa.manager.domain.Account;
import com.bofa.manager.domain.Customer;
import com.bofa.manager.domain.UserLogin;
import com.excel.operations.ExcelCreator;
import com.excel.operations.ExcelReader;
import com.excel.operations.FormatType;
import com.excel.operations.ReportColumn;

public class TestDataExcelDaoImpl implements TestDataDao {
	static Logger logger = Logger.getLogger(TestDataExcelDaoImpl.class.getName());
	
	/* (non-Javadoc)
	 * @see com.bofa.dao.TestDataDao#getLoginUserData()
	 */
	@Override
	public List<UserLogin> getLoginUserData(){
		
		logger.info("Getting Current ExcelData.. ");
		int excelSheetNumber = 1;
		int noOfRowsToSkipSheet1 = 1;
		
		String excelFileName = "InputTestData/TestData.xlsx";
		
		Map<String, String> excelColFieldMappingSheet3 = new HashMap<String, String>();
		excelColFieldMappingSheet3.put("A", "testCaseNo");
		excelColFieldMappingSheet3.put("B", "category");
		excelColFieldMappingSheet3.put("C", "username");
		excelColFieldMappingSheet3.put("D", "password");
		excelColFieldMappingSheet3.put("E", "testStatus");
		
		logger.info("Printing Current Excel Data.");
		
		ExcelReader<UserLogin> excelReader = new ExcelReader<UserLogin>(noOfRowsToSkipSheet1);
		List<UserLogin> userLogins = excelReader.getObjects(excelFileName, excelSheetNumber, UserLogin.class, excelColFieldMappingSheet3);
		for (UserLogin user : userLogins) {
			logger.info("Excel data user="+user);
		}
		
		logger.info("Completed Reading Current Excel Data.");
		
		return userLogins;
	}
	
	/* (non-Javadoc)
	 * @see com.bofa.dao.TestDataDao#saveUserLoginTestResults(java.util.List, java.lang.String)
	 */
	@Override
	public void saveUserLoginTestResults(List<UserLogin> userLogins, String browser) throws ParseException, IOException {
		String fileName = String.format("InputTestData/%s-%s.xlsx", browser, "userLoginTestResults");
		logger.info("Test results file "+fileName);
		
		// Create an array of report column objects
        ReportColumn[] reportColumns = new ReportColumn[] {
                new ReportColumn("testCaseNo", "Test Case#", FormatType.TEXT),
                new ReportColumn("category", "Category", FormatType.TEXT),
                new ReportColumn("username", "User Name", FormatType.TEXT),
                new ReportColumn("password", "User Password", FormatType.TEXT),
                new ReportColumn("testStatus", "Status", FormatType.TEXT)
                };
        
        ExcelCreator excelCreator = new ExcelCreator(fileName);
        excelCreator.addSheet(userLogins, reportColumns, "userLogin");
        excelCreator.create();
	}
	
	/* (non-Javadoc)
	 * @see com.bofa.dao.TestDataDao#saveCreateNewCustomerTestResults(java.util.List, java.lang.String)
	 */
	@Override
	public void saveCreateNewCustomerTestResults(List<Customer> cuustomerTestData, String browser) throws ParseException, IOException {
		String fileName = String.format("InputTestData/%s-%s.xlsx", browser, "createNewCustomerTestResults");
		logger.info("Test results file "+fileName);
		
		// Create an array of report column objects
        ReportColumn[] reportColumns = new ReportColumn[] {
                new ReportColumn("testCaseNo", "Test Case#", FormatType.TEXT),
                new ReportColumn("category", "Category", FormatType.TEXT),
                
                new ReportColumn("name", "Name", FormatType.TEXT),
                new ReportColumn("gender", "Gender", FormatType.TEXT),
                new ReportColumn("dob", "Dob", FormatType.TEXT),
                new ReportColumn("address", "Address", FormatType.TEXT),
                new ReportColumn("city", "City", FormatType.TEXT),
                new ReportColumn("state", "State", FormatType.TEXT),
                new ReportColumn("pin", "Pin", FormatType.TEXT),
                new ReportColumn("telephone", "Telephone", FormatType.TEXT),
                new ReportColumn("email", "Telephone", FormatType.TEXT),
                new ReportColumn("password", "Telephone", FormatType.TEXT),
                
                new ReportColumn("customerId", "CustomerId", FormatType.TEXT),
                
                new ReportColumn("testStatus", "Status", FormatType.TEXT)
                };
        
        ExcelCreator excelCreator = new ExcelCreator(fileName);
        excelCreator.addSheet(cuustomerTestData, reportColumns, "addCustomer");
        excelCreator.create();
	}
	
	//Data for Create New customer
	/* (non-Javadoc)
	 * @see com.bofa.dao.TestDataDao#getCreateNewCustomer()
	 */
	@Override
	public List<Customer> getCreateNewCustomer(){
		
		logger.info("Getting Current ExcelData.. ");
		int excelSheetNumber = 2;
		int noOfRowsToSkipSheet2 = 1;
		
		String excelFileName = "InputTestData/TestData.xlsx";
		
		Map<String, String> excelColFieldMappingSheet2 = new HashMap<String, String>();
		excelColFieldMappingSheet2.put("A", "testCaseNo");
		excelColFieldMappingSheet2.put("B", "category");
		excelColFieldMappingSheet2.put("C", "name");
		excelColFieldMappingSheet2.put("D", "gender");
		excelColFieldMappingSheet2.put("E", "dob");
		excelColFieldMappingSheet2.put("F", "address");
		excelColFieldMappingSheet2.put("G", "city");
		excelColFieldMappingSheet2.put("H", "state");
		excelColFieldMappingSheet2.put("I", "pin");
		excelColFieldMappingSheet2.put("J", "telephone");
		excelColFieldMappingSheet2.put("K", "email");
		excelColFieldMappingSheet2.put("L", "password");
		excelColFieldMappingSheet2.put("M", "testStatus");
		
		logger.info("Printing Current Excel Data.");
		
		ExcelReader<Customer> excelReader = new ExcelReader<Customer>(noOfRowsToSkipSheet2);
		List<Customer> newCustomer = excelReader.getObjects(excelFileName, excelSheetNumber, Customer.class, excelColFieldMappingSheet2);
		for (Customer customer : newCustomer) {
			logger.info("Excel data user="+ customer);
		}
		
		logger.info("Completed Reading Current Excel Data.");
		
		return newCustomer;
	}
	//Data for add new account
/* (non-Javadoc)
 * @see com.bofa.dao.TestDataDao#geAddNewAccountData()
 */
@Override
public List<Account> geAddNewAccountData(){
		
		logger.info("Getting Current ExcelData.. ");
		int excelSheetNumber = 3;
		int noOfRowsToSkipSheet3 = 1;
		
		String excelFileName = "InputTestData/TestData.xlsx";
		
		Map<String, String> excelColFieldMappingSheet3 = new HashMap<String, String>();
		excelColFieldMappingSheet3.put("A", "testCaseNo");
		excelColFieldMappingSheet3.put("B", "category");
		excelColFieldMappingSheet3.put("C", "customerId");
		excelColFieldMappingSheet3.put("D", "accountType");
		excelColFieldMappingSheet3.put("E", "initialDeposit");
		excelColFieldMappingSheet3.put("F", "username");
		excelColFieldMappingSheet3.put("G", "accountId");
		excelColFieldMappingSheet3.put("H", "testStatus");
		
		logger.info("Printing Current Excel Data.");
		
		ExcelReader<Account> excelReader = new ExcelReader<Account>(noOfRowsToSkipSheet3);
		List<Account> accounts = excelReader.getObjects(excelFileName, excelSheetNumber, Account.class, excelColFieldMappingSheet3);
		for (Account account : accounts) 
			
		 {
			logger.info("Excel data user="+ account);
		}
		
		logger.info("Completed Reading Current Excel Data.");
		
		return accounts;
	}
//Test Results for Create new Account
/* (non-Javadoc)
 * @see com.bofa.dao.TestDataDao#saveAddNewAccountsTestResults(java.util.List, java.lang.String)
 */
@Override
public void saveAddNewAccountsTestResults(List<Account> accountTestData, String browser) throws ParseException, IOException {
	String fileName = String.format("InputTestData/%s-%s.xlsx", browser, "AddNewAccountTestResults");
	logger.info("Test results file "+fileName);
	
	// Create an array of report column objects
    ReportColumn[] reportColumns = new ReportColumn[] {
            new ReportColumn("testCaseNo", "Test Case#", FormatType.TEXT),
            new ReportColumn("category", "Category", FormatType.TEXT),
            new ReportColumn("customerId", "CustomerId", FormatType.TEXT),
            new ReportColumn("accountType", "AccountType", FormatType.TEXT),
            new ReportColumn("initialDeposit", "InitialDeposit", FormatType.TEXT),
            new ReportColumn("username", "UserName", FormatType.TEXT),
            new ReportColumn("accountId", "AccountId", FormatType.TEXT),
            new ReportColumn("testStatus", "Status", FormatType.TEXT)
            };
    
    ExcelCreator excelCreator = new ExcelCreator(fileName);
    excelCreator.addSheet(accountTestData, reportColumns, "addNewAccount");
    excelCreator.create();
}
}
